from django.contrib import admin

from django.contrib import admin
from .models import Product, ProductMainCategory, ProductSubCategory

# 商品大类Admin
class ProductMainCategoryAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'sort_order', 'desc')  # 列表显示的字段
    search_fields = ('name',)  # 搜索字段
    list_editable = ('sort_order',)  # 列表页可编辑字段
    ordering = ('sort_order', 'id')  # 默认排序

# 商品小类Admin
class ProductSubCategoryAdmin(admin.ModelAdmin):
    list_display = ('id', 'main_category', 'name', 'sort_order', 'desc')  # 列表显示的字段
    search_fields = ('name', 'main_category__name')  # 搜索字段（支持跨表搜索）
    list_filter = ('main_category',)  # 过滤字段
    list_editable = ('sort_order',)  # 列表页可编辑字段
    ordering = ('main_category', 'sort_order', 'id')  # 默认排序

# 商品Admin
class ProductAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'barcode', 'sub_category', 'price', 'stock', 'is_hot', 'create_time', 'update_time')  # 列表显示的字段
    search_fields = ('name', 'barcode', 'sub_category__name', 'sub_category__main_category__name')  # 搜索字段
    list_filter = ('sub_category__main_category', 'sub_category', 'is_hot')  # 过滤字段
    list_editable = ('price', 'stock', 'is_hot')  # 列表页可编辑字段
    ordering = ('-create_time',)
    # 详细页面字段分组
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'barcode', 'sub_category', 'price')
        }),
        ('库存与状态', {
            'fields': ('stock', 'is_hot')
        }),
        ('其他信息', {
            'fields': ('image',)
        }),
    )

# 注册模型到Admin界面
admin.site.register(ProductMainCategory, ProductMainCategoryAdmin)
admin.site.register(ProductSubCategory, ProductSubCategoryAdmin)
admin.site.register(Product, ProductAdmin)
